/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.plugin.portunis.impl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import nu.zoom.swing.desktop.Workbench;
import nu.zoom.swing.layout.VerticalPanel;

import org.ops4j.gaderian.Messages;

/**
 * A Dialog to get the password for a key.
 * 
 * @version $Revision: 1.1 $
 * @author $Author: johan $
 */
class KeyPasswordDialog extends JDialog
{

	private JPasswordField password;
	private boolean closeOK = false;

	KeyPasswordDialog(Workbench workbench, Messages messages) {
		super(workbench.getDialogOwner(), messages
				.getMessage("keystore.key.passphrase.title"), true);
		setLocationRelativeTo(workbench.getDialogOwner());

		JLabel passLabel = new JLabel(messages
				.getMessage("keystore.key.passphrase.label"));
		password = new JPasswordField(25);
		password.setToolTipText(messages
				.getMessage("keystore.key.passphrase.tooltip"));
		VerticalPanel mainLayout = new VerticalPanel();
		mainLayout.addRow(passLabel, password);

		JButton okButton = new JButton(messages
				.getMessage("keystore.key.passphrase.ok"));
		okButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e)
			{
				closeOK = true;
				dispose();
			}
		});
		JButton cancelButton = new JButton(messages
				.getMessage("keystore.general.cancel"));
		cancelButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e)
			{
				closeOK = false;
				dispose();
			}
		});
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		mainLayout.addRow(buttonPanel);

		getContentPane().add(mainLayout);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}

	boolean isCloseOK()
	{
		return closeOK;
	}

	char[] getPassword()
	{
		return password.getPassword();
	}
}
